//
//  CNStatusOriginalView.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/26.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit
import SnapKit

class CNStatusOriginalView: UIView {
    
    var status: CNStatus? {
        didSet {
            if let url = NSURL(string: (status?.user?.profile_image_url)!) {
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            nameLabel.text = status?.user?.screen_name
            timeLabel.text = NSDate.dateFromSinaString(status?.created_at ?? "").sinaDisplayString()
            sourceLabel.text = status?.source
            memberView.image = status?.user?.mbrankImage
            verifyView.image = status?.user?.verified_typeImage
            contentLabel.attributedText = status?.attrText
            
            originalViewBottomConstraint?.uninstall()
            if let pictureURLs = status?.pictureURLs {
                picturesView.hidden = false
                // 传值
                picturesView.pictureURLs = pictureURLs
                self.snp_makeConstraints(closure: { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(picturesView).offset(defaultMargin).constraint
                })
            } else {
                picturesView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(defaultMargin).constraint
                })
            }
        }
    }
    // OriginalView底部约束
    private var originalViewBottomConstraint: Constraint?

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame) 
        prepareUI()
    }
    
    private func prepareUI() {
        
        backgroundColor = UIColor.whiteColor()
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifyView)
        addSubview(contentLabel)
        addSubview(picturesView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(defaultMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(defaultMargin)
            make.top.equalTo(iconView)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(defaultMargin)
            make.bottom.equalTo(iconView)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(defaultMargin)
            make.centerY.equalTo(timeLabel)
        }
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(defaultMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        verifyView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(defaultMargin)
            make.trailing.equalTo(self).offset(-defaultMargin)
            make.top.equalTo(iconView.snp_bottom).offset(defaultMargin)
        }
        picturesView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(defaultMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(defaultMargin)
            make.size.equalTo(CGSize(width: 200, height: 80))
        }
        self.snp_makeConstraints { (make) -> Void in
            originalViewBottomConstraint = make.bottom.equalTo(picturesView).offset(defaultMargin).constraint
        }
    }
    // MARK: - lazyLoad
    /// 用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    /// 用户昵称
    private lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(textFontLarge)
        return label
    }()
    /// 时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(textFontSmall)
        return label
    }()
    /// 来源
    private lazy var sourceLabel: UILabel = {
       
        let label = UILabel()
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(textFontSmall)
        return label
    }()
    /// 会员等级
    private lazy var memberView: UIImageView = UIImageView()
    /// 认证类型
    private lazy var verifyView: UIImageView = UIImageView()
    /// 微博内容
    private lazy var contentLabel: FFLabel = {
        
        let label = FFLabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(textFontLarge)
        label.numberOfLines = 0
        // 解决label高度计算不准确问题
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * defaultMargin
        return label
    }()
    /// 微博配图
    private lazy var picturesView: CNStatusPicturesView = CNStatusPicturesView()
}
